﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace UdrinkWater_Project.Admin
{
    public partial class DetailManuFacture : System.Web.UI.Page
    {
        private string Connection = ConfigurationManager.ConnectionStrings["DB_Udrink"].ConnectionString;
        protected void Page_Load(object sender, EventArgs e)
        {
            DataTable dtInList = GetInList();
            ASPxGridView1.DataSource = dtInList;
            ASPxGridView1.DataBind();
            //ddddddd
        }
        private DataTable GetInList()
        {
            string sqltext = @"SELECT DISTINCT TB_Manufacture.Manu_Date, TB_Manufacture.Act_Officer,
(TB_Employee.Prefix+''+ TB_Employee.Firstname + '' +TB_Employee.Lastname) AS EmpName, TB_Manufacture.Status_Manufacture,
TB_Status_Manufacture.Status_Manufacture_Name FROM TB_Employee INNER JOIN TB_Manufacture ON TB_Employee.ID_Card = TB_Manufacture.Act_Officer INNER JOIN TB_Status_Manufacture ON TB_Manufacture.Status_Manufacture = TB_Status_Manufacture.Status_Manufactue_ID WHERE TB_Manufacture.Status_Manufacture = 2";
            SqlConnection sqlconn = new SqlConnection(Connection);
            SqlDataAdapter da = new SqlDataAdapter(sqltext, sqlconn);
            DataSet ds = new DataSet();
            da.Fill(ds);
            return ds.Tables[0];
        }


       
        protected void ASPxGridView1_RowCommand(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewRowCommandEventArgs e)
        {
            if (e.CommandArgs.CommandName == "Cmd_Select")
            {
                string Id = ChangeYear(e.CommandArgs.CommandArgument.ToString());
                Session["ManuDateID"] = Id;
                Response.Redirect("BillManu.aspx");

            }
            if (e.CommandArgs.CommandName == "Cmd_Cancle")
            {
                string Id = ChangeYear(e.CommandArgs.CommandArgument.ToString());
                string sqltext = @" UPDATE [TB_Manufacture]
                                    SET    [Status_Manufacture] = 1
                                    WHERE [Manu_Date] = '"+Id+"'";
                        SqlCommand sqlcomm = new SqlCommand();
                        SqlConnection sqlconn = new SqlConnection(Connection);
                        sqlcomm.Connection = sqlconn;
                        sqlcomm.CommandType = System.Data.CommandType.Text;
                        sqlcomm.CommandText = sqltext;
                        try
                        {
                            sqlcomm.Connection.Open();
                            sqlcomm.ExecuteNonQuery();
                        }
                        catch (Exception)
                        {
                            sqlcomm.Connection.Close();
                            sqlcomm.Connection.Dispose();
                        }
                        sqlcomm.Connection.Close();
                        sqlcomm.Connection.Dispose();
                        DataTable dtInList = GetInList();
                        ASPxGridView1.DataSource = dtInList;
                        ASPxGridView1.DataBind();

            }
        }
        private string ChangeYear(string InDate)
        {
            string day = "";
            string mount = "";
            string years = "";
            try
            {
                try
                {
                    string[] stemp = InDate.Split('-');
                    years = (stemp[0]);
                    mount = (stemp[1]);
                    day = (stemp[2]);


                }
                catch (Exception)
                {

                    string[] stemp = InDate.Split('/');
                    years = (stemp[0]);
                    mount = (stemp[1]);
                    day = (stemp[2]);

                }


                if (int.Parse(years) > 2300)
                {
                    years = (int.Parse(years) - 543).ToString();
                }
                return (years + "-" + mount + "-" + day);
            }
            catch (Exception)
            {

            }

            return (years + "-" + mount + "-" + day);
        }
    }
}